<template>
	<view class="form-container">
		<nt-order-detail :orderInfo="commonForm.orderInfo"></nt-order-detail>
		<nt-card label="基本信息">
			<view class="form-item horizontal">
				<view class="form-item__label">自提点：</view>
				<view class="form-item__content" @tap="show = true">
					<u--input v-model="formData.picked" readonly placeholder="请选择自提点"></u--input>
				</view>
			</view>
			<!-- <view class="form-item horizontal">
				<view class="form-item__label">自提备注：</view>
				<view class="form-item__content">
					<u--input v-model="formData.pickedRemarks" placeholder="请选择自提备注"></u--input>
				</view>
			</view> -->
			<view class="form-item horizontal" v-if="key == 5">
				<view class="form-item__label">要好评：</view>
				<view class="form-item__content">
					<u-radio-group v-model="formData.yaohaoping">
						<u-radio label="可以验券" :name="1" :customStyle="{marginRight: '20rpx'}"></u-radio>
						<u-radio label="不可以验券" :name="2"></u-radio>
					</u-radio-group>
				</view>
			</view>
			<view class="form-item vertical">
				<view class="form-item__label">备注信息：</view>
				<view class="form-item__content">
					<u--textarea v-model="formData.remarks" maxlength="-1" :confirmType="null"></u--textarea>
				</view>
			</view>
			<view class="form-item vertical">
				<view class="form-item__label">上传图片：</view>
				<view class="form-item__content">
					<nt-upload @success="successUpload" :images="previewImage"></nt-upload>
				</view>
			</view>
			<nt-button text="提交" @tap="submit"></nt-button>
		</nt-card>
		<u-picker :show="show" :columns="columns" keyName="name" @cancel="show = false" @confirm="confirm" @close="show = false" closeOnClickOverlay></u-picker>
	</view>
</template>

<script>
import { selfPickList, applyRepatriation, getCommonFormConfig } from "@/api"
import { orderTypeEnum } from "@/utils/enum"
import { authMap } from "@/utils"
export default {
	props: {
		orderInfo: {
			type: Object,
			default: () =>{}
		},
		config: {
			type: Object,
			default: () =>{}
		},
		options: {
			type: Object,
			default: () =>{}
		},
	},
	data(){
		return {
			formData: {
				// 备注
				remarks: "",
				// 自提点
				picked: "",
				// 自提点备注
				pickedRemarks: "",
				yaohaoping: null
			},
			show: false,
			columns: [],
			images: [],
			previewImage: [],
			selfList: [],
			// 申请类型
			type: "",
			orderId: "",
			key: 0,
			loading:false
		}
	},
	watch: {
		orderInfo: {
			handler(n){
				if(n && n.applyreturn){
					const find = n.applyreturn.find(item => {
							return this.commonForm.options.return_id == item.id
							// console.log(authMap.get(item.is_type).key, this.key, 'find==')
							// return authMap.get(item.is_type).key === this.key
					})
					if(find){
						this.formData.remarks = find.content
						this.formData.yaohaoping = find.yaohaoping
						this.formData.picked = find.picked
						this.formData.pickedRemarks = find.pickedRemarks
						this.previewImage = find.images_list || []
					}
				}
			},
			deep: true,
		},
		config: {
			handler(n){
				this.columns = [n.pickedList]
			},
			deep: true
		}
	},
	inject: ["commonForm"],
	mounted(){
		let key = Number(this.commonForm.options.key)
		this.type = orderTypeEnum[key]
		this.key = key
		// this.getFormConfig()
		
	},
	methods: {
		confirm(e){
			this.formData.picked = e.value[0].value
			this.show = false
		},
		// 上传成功
		successUpload(e){
			this.images = e
		},
		// 获取配件
		getFormConfig(type){
			getCommonFormConfig(this.commonForm.options.id).then(res => {
				const { code, data } = res;
				if(code !== 1) return;
				this.formConfig = data
				this.columns = [data.pickedList]

			})
		},
		// 提交
		submit(){
			// 返修不需要上传图片
			console.log(this.images,'-----')
			const { formData: {
				remarks, picked, pickedRemarks, yaohaoping
			}, orderId, type, images, key } = this;
			const imageList = images.map(item => item.url)
			if(key == 5 && !yaohaoping) return this.toast("请选择要好评类型")
			if(!remarks) return this.toast("请填写备注信息")
			if(!imageList.length && ![1, 3, 5].includes(this.key)) return this.toast("请上传图片")
			if(this.loading) return
			this.loading = true
			const formData = {
				realorder_id: this.commonForm.options.id,
				is_type: type,
				remarks,
				images: imageList,
				picked,
				pickedRemarks,
				yaohaoping
			}
			if(this.commonForm.options.return_id){
				formData.return_id = this.commonForm.options.return_id
			}
			applyRepatriation(formData).then(res => {
				const { code, msg } = res
				if(code === 1){
					this.toast("申请已提交")
					setTimeout(() => this.back(), 1000)
				}
			}).finally(() => {
				this.loading = false
			})
		}
	}
}
</script>

<style scoped lang="scss">
@import "./style.scss";
</style>